package com.tianhui.consignortwofei.other.auth.recognition;

import android.content.Context;

import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.BankCardParams;
import com.baidu.ocr.sdk.model.BankCardResult;
import com.baidu.ocr.sdk.model.IDCardResult;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.widget.CustomProgressDialog;
import com.tianhui.consignortwofei.mvp.model.enty.RecognitionInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.other.auth.inte.RecognitionCallback;
import com.tianhui.consignortwofei.utils.ApplicationUtil;

import java.io.File;

/**
 * @author fgs
 * @date 2021/5/13
 * @description
 **/
public class BankCardRecognition implements IRecognition<BankCardResult> {


    @Override
    public void recognition(Context context, SelectPhotoInfo selectPhotoInfo, RecognitionCallback<BankCardResult> callback) {
        BankCardParams param = new BankCardParams();
        param.setImageFile(new File(selectPhotoInfo.filePath));

        CustomProgressDialog customProgressDialog = new CustomProgressDialog(context);
        customProgressDialog.show();

        OCR.getInstance(ApplicationUtil.getContext()).recognizeBankCard(param, new OnResultListener<BankCardResult>() {
            @Override
            public void onResult(BankCardResult result) {
                customProgressDialog.dismiss();
                LogUtil.print(result.toString());
                if (result != null) {
                    RecognitionInfo recognitionInfo = new RecognitionInfo<IDCardResult>();
                    recognitionInfo.info = result;
                    recognitionInfo.filePath = selectPhotoInfo.filePath;
                    if (null != callback)
                        callback.recognitionSuccess(recognitionInfo);
                } else {
                    if (null != callback) {
                        callback.recognitionFailed();
                    }
                }
            }

            @Override
            public void onError(OCRError error) {
                customProgressDialog.dismiss();
                if (null != callback)
                    callback.recognitionFailed();
            }
        });

    }
}
